package com.lagou.homework;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class PutData {

    Configuration conf = null;
    Connection conn = null;
    HBaseAdmin admin = null;

    @Before
    public void init() throws IOException {
        conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum", "node01,node02");
        conf.set("hbase.zookeeper.property.clientPort", "2181");
        conn = ConnectionFactory.createConnection(conf);
    }

    @Test
    public void createTable() throws IOException {

        admin = (HBaseAdmin)conn.getAdmin();

        HTableDescriptor teacher = new HTableDescriptor(TableName.valueOf("friendShip"));
        teacher.addFamily(new HColumnDescriptor("friends"));

        admin.createTable(teacher);

        System.out.println("friendShip表创建成功");
    }

    @Test
    public void prepareData() throws IOException {

        //声明好友关系
        HashMap<String, List<String>> friendsShip = new HashMap<>();

        ArrayList<String> uid1 = new ArrayList<>();
        uid1.add("uid2");
        uid1.add("uid4");
        uid1.add("uid5");
        friendsShip.put("uid1", uid1);


        ArrayList<String> uid2 = new ArrayList<>();
        uid2.add("uid1");
        uid2.add("uid3");
        friendsShip.put("uid2", uid2);

        ArrayList<String> uid3 = new ArrayList<>();
        uid3.add("uid2");
        uid3.add("uid4");
        friendsShip.put("uid3", uid3);

        ArrayList<String> uid4 = new ArrayList<>();
        uid4.add("uid1");
        uid4.add("uid3");
        uid4.add("uid5");
        friendsShip.put("uid4", uid4);

        ArrayList<String> uid5 = new ArrayList<>();
        uid5.add("uid1");
        uid5.add("uid4");
        friendsShip.put("uid5", uid5);

        Table t = conn.getTable(TableName.valueOf("friendShip"));


        for (String uid:friendsShip.keySet()) {

            Put put = new Put(Bytes.toBytes(uid));
            List<String> friends = friendsShip.get(uid);
            for (String friend : friends) {
                put.addColumn(Bytes.toBytes("friends"), Bytes.toBytes(friend), Bytes.toBytes(friend));
            }

            t.put(put);
        }

        t.close();
    }

    /**
     * 删除好友关系, 例如: 从uid1的好友关系中删除uid5
     */
    @Test
    public void deleteFriendShip() throws IOException {

        Table friendShip = conn.getTable(TableName.valueOf("friendShip"));

        Delete uid1 = new Delete(Bytes.toBytes("uid1"));
        uid1.addColumn(Bytes.toBytes("friends"), Bytes.toBytes("uid5"));

        friendShip.delete(uid1);
        System.out.println("从uid1的好友中删掉了uid5");

        Delete uid5 = new Delete(Bytes.toBytes("uid5"));
        uid5.addColumn(Bytes.toBytes("friends"), Bytes.toBytes("uid1"));

        friendShip.delete(uid5);
        System.out.println("然后再从uid5的好友中删掉uid1");
    }

    @After
    public void destroy() {

        if(admin!=null){
            try {
                admin.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if(conn !=null){
            try {
                conn.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
